﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace QLBN
{
    public partial class frmTTBenhNhan : Form
    {

        DBConnector dbc = new DBConnector();
        Boolean ck = false; //Kiem tra xem toa thuoc co ton tai chua
        internal Boolean cksuadl = false;
        frmmain frmParent;
        internal int cktab = 0;
        string KhangSinh = "";



        public frmTTBenhNhan()
        {
            InitializeComponent();
        }
        public frmTTBenhNhan(frmmain frm)
        {
            InitializeComponent();
            frmParent = frm;
        }
        private void txttenbn_KeyUp(object sender, KeyEventArgs e)
        {
            //if (this.txttenbn.Text != "")
            //{
                //if (this.txttenbn.Text.Replace(" ", "") != "")
                //{
                //    DataSet ds = null;
                //    BenhNhan bn = new BenhNhan();
                //    lbox_tenbn.Items.Clear();
                //    ds = bn.Tim_Benh_Nhan(this.txttenbn.Text);
                //    if (ds.Tables[0].Rows.Count >= 1)
                //    {
                //        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                //        {
                //            lbox_tenbn.Items.Add(ds.Tables[0].Rows[i][1].ToString());
                //        }
                //        lbox_tenbn.Visible = true;

                //        if (e.KeyValue == 13)   // Phim enter
                //        {
                //            lbox_tenbn.Visible = false;
                //            Hien_Thong_Tin();
                //        }

                //        else if (e.KeyValue == 40)  // phim arrow down
                //        {
                //            lbox_tenbn.Select();
                //            lbox_tenbn.SelectedIndex = 0;
                //        }
                //    }
                //}
                //else

            if (this.txttenbn.Text.Replace(" ", "") == "")
            {
                //this.lbox_tenbn.Visible = false;
                Hien_Thong_Tin();
            }
            //}
            //else
            //{
            //    //this.lbox_tenbn.Visible = false;
            //    Hien_Thong_Tin();
            //}

            //if (lbox_tenbn.Items.Count <= 0)
            //{
            //    lbox_tenbn.Visible = false;
            //}
            
        }
        private void lbox_tenbn_SelectedValueChanged(object sender, EventArgs e)
        {
            if (lbox_tenbn.SelectedIndex != -1)
            {
                this.txttenbn.Text = lbox_tenbn.Items[lbox_tenbn.SelectedIndex].ToString();
            }
        }

        private void lbox_tenbn_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                lbox_tenbn.Visible = false;
                txttenbn.Focus();
                Hien_Thong_Tin();
            }
        }

        private void Hien_Thong_Tin()
        {
            BenhNhan bn = new BenhNhan();
            String sql = "Select MaBN from BenhNhan where TenBN = '" + this.txttenbn.Text + "'";
            DataSet ds = dbc.executeQuery(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                //Phan thong tin ca nhan benh nhan
                String mabn = ds.Tables[0].Rows[0][0].ToString();
                sql = "Select * from BenhNhan where MaBN = " + mabn;
                ds = dbc.executeQuery(sql);
                this.txtmabn.Text = ds.Tables[0].Rows[0][0].ToString();
                this.txttenbn.Text = ds.Tables[0].Rows[0][1].ToString();
                DateTime d = Convert.ToDateTime(ds.Tables[0].Rows[0][2].ToString());
                String str = "";
                
                if (d.Day < 10)
                {
                    str += "0" + d.Day;
                }
                else
                {
                    str += d.Day;
                }
                if (d.Month < 10)
                {
                    str += "0" + d.Month;
                }
                else
                {
                    str += d.Month;
                }
                str += d.Year;
                this.txttuoibn.Text = str;
                //this.txttuoibn1.Text = ds.Tables[0].Rows[0][2].ToString();
                this.txtcannang.Text = ds.Tables[0].Rows[0][3].ToString();
                this.txtdiachi.Text = ds.Tables[0].Rows[0][4].ToString();
                this.txtbenhsu.Text = ds.Tables[0].Rows[0][5].ToString();
                this.txtdiung.Text = ds.Tables[0].Rows[0][6].ToString();
                this.txtkhangthuoc.Text = ds.Tables[0].Rows[0][7].ToString();
                this.txtghichu.Text = ds.Tables[0].Rows[0][8].ToString();
                //Phan hien thi cac loai thuoc dap ung
                sql = "Select Tot, Vua, Khong from DapUng where MaBN = " + mabn;
                ds = dbc.executeQuery(sql);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    this.txtdugtot.Text = ds.Tables[0].Rows[0][0].ToString();
                    this.txtdugvua.Text = ds.Tables[0].Rows[0][1].ToString();
                    this.txtdugkhong.Text = ds.Tables[0].Rows[0][2].ToString();
                }
                else
                {
                    this.txtdugtot.Text = "";
                    this.txtdugvua.Text = "";
                    this.txtdugkhong.Text = "";
                }
                ////Phan hien thi cac loai thuoc phoi hop
                sql = "Select PHMot, PHHai,PHBa From PhoiHop where MaBN = " + mabn;
                ds = dbc.executeQuery(sql);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    this.txtphlanmot.Text = ds.Tables[0].Rows[0][0].ToString();
                    this.txtphlanhai.Text = ds.Tables[0].Rows[0][1].ToString();
                    this.txtphlanba.Text = ds.Tables[0].Rows[0][2].ToString();
                }
                else
                {
                    this.txtphlanmot.Text = "";
                    this.txtphlanhai.Text = "";
                    this.txtphlanba.Text = "";
                }
                //Phan hien thi thong tin 2 toa thuoc moi nhat
                sql = "Select TOP 2 MaToa from ToaThuoc where MaBN = " + mabn;
                sql += " Order By MaToa DESC";
                ds = dbc.executeQuery(sql);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count == 1)
                    {
                        String matoa = ds.Tables[0].Rows[0][0].ToString();
                        sql = "Select TenThuoc, LieuDung, ThoiGian, GhiChu from ChiTietToaThuoc ";
                        sql += "where MaToa = " + matoa;
                        ds = dbc.executeQuery(sql);
                        this.dgrvToa1.DataSource = ds.Tables[0];
                        //this.dgrvToa1.DataSource = null;
                    }
                    else if (ds.Tables[0].Rows.Count == 2)
                    {
                        String matoa1 = ds.Tables[0].Rows[0][0].ToString();
                        String matoa2 = ds.Tables[0].Rows[1][0].ToString();
                        sql = "Select TenThuoc, LieuDung, ThoiGian, GhiChu from ChiTietToaThuoc ";
                        sql += "where MaToa = " + matoa1;
                        ds = dbc.executeQuery(sql);
                        this.dgrvToa1.DataSource = ds.Tables[0];

                        sql = "Select TenThuoc, LieuDung, ThoiGian, GhiChu from ChiTietToaThuoc ";
                        sql += "where MaToa = " + matoa2;
                        ds = dbc.executeQuery(sql);
                        this.dgrvToa2.DataSource = ds.Tables[0];
                    }

                }
                else
                {
                    this.dgrvToa1.DataSource = null;
                    this.dgrvToa2.DataSource = null;
                }

                ds = bn.Lay_Danh_Sach_Toa(mabn);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    dgvtoathuoc.DataSource = ds.Tables[0];
                    dgvchitiettoa.AllowUserToAddRows = true;
                }
                else
                {
                    masktxtdate.Text = "";

                    ds = bn.Lay_Danh_Sach_Toa("-1");
                    dgvtoathuoc.DataSource = ds.Tables[0];
                    
                    ds = bn.Lay_Danh_Sach_Chi_Tiet_Toa("-1");
                    dgvchitiettoa.DataSource = ds.Tables[0];
                    dgvchitiettoa.AllowUserToAddRows = false;
                }
            }
            else
            {
                Xoa_Form();
            }
            cksuadl = false;
            BenhNhan.cksuadl = false;
        }


        string tenthuoc = "",tenthuoc1 = "";
        internal void Hien_Thong_Tin(String MaBN)
        {
            BenhNhan bn = new BenhNhan();
            String mabn = MaBN;
            String sql = "Select * from BenhNhan where MaBN = " + mabn;
            DataSet ds = dbc.executeQuery(sql);
            this.txtmabn.Text = ds.Tables[0].Rows[0][0].ToString();
            this.txttenbn.Text = ds.Tables[0].Rows[0][1].ToString();
            DateTime d = Convert.ToDateTime(ds.Tables[0].Rows[0][2].ToString());
            String str = "";
            
            
            if (d.Day < 10)
            {
                str += "0" + d.Day;
            }
            else
            {
                str += d.Day;
            }
            if (d.Month < 10)
            {
                str += "0" + d.Month;
            }
            else
            {
                str += d.Month;
            }
            str += d.Year;
            this.txttuoibn.Text = str;
            //this.txttuoibn1.Text = ds.Tables[0].Rows[0][2].ToString();
            this.txtcannang.Text = ds.Tables[0].Rows[0][3].ToString();
            this.txtdiachi.Text = ds.Tables[0].Rows[0][4].ToString();
            this.txtbenhsu.Text = ds.Tables[0].Rows[0][5].ToString();
            this.txtdiung.Text = ds.Tables[0].Rows[0][6].ToString();





            //this.txtkhangthuoc.Text = ds.Tables[0].Rows[0][7].ToString();
            KhangSinh = "Keldacin@Nakai@Zucer@Koat@Klamentin250@Klamentin500@Kla500@Kla250@Clarytromycine@Clari250@clari500@Rulid@Roxytromycine@Roxi@Erytromycine@Ery@Azicin@Azitral@Cromazin@Azi@Doromax@Azitrin@Rovamycin@Rova3M@Rova0,75@Rova1,5M@Rova0.75@Rova1.5M@Cefaclor500@Cefaclor250@Cefaclor125@Cefaclor@Ranclor@Imexclor@Vercef125@Vercef250@Cepo100@Cepo200@Ceficim50@Ceficim100@Cefi@Cimodo@Kivacef@Okenxim@Fixcap@Cemax@Unifix@Furocap@Doroxim250@Zinat@Cefdixim@Cefuroxim@Doroxim500@Negacef@Imedroxil@Biodroxyl@Nircef@Cefdinir@Altamet500@Altamet250@Cetamet@Clindas@Linmadin@Oxalipen@Bristopen@Opecipro@Scanax@Openacin@Levof@Levoday250@Uni@Volexin@Levoday500@Negram@Ciprex@Clamox@Augmex1g@Augmex625@Klamentin@Dodacin@Rezotum@Trifamox@Đ@ĐN@XX@TU@UI@N@O@Q@QI@ĐL@QO@G@GV@GV@V@QG@K@KN@NA@NB@KB@KI@VS@EL@CNN@FP@CN5@CN@TE@ES@TVV@USP@T@TB@TV@KC@37@SSn@SS@X@A@I@IV@XT@RA@HN@CT@O@LAB@LAG@CPT@CPV@W@HC@CP@9N@9H@9L@9V@CAL@ET@RO";
            string[] a = KhangSinh.Split('@');
            String  sql1 = "Select * from ToaThuoc where MaBN = " + mabn;
            DataSet  ds1 = dbc.executeQuery(sql1);
            DataSet ds2 = new DataSet(),ds3 = new DataSet();
            string Ngaymua = "";
            string toake1 = "", toake2 = "";
            string toake = "";
            int temp;
            string t;
            string matoa = "";
            for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
            {
                String  sql2 = "Select * from ChiTietToaThuoc where MaToa = " + ds1.Tables[0].Rows[i][0].ToString();
                ds2 = dbc.executeQuery(sql2);
                Ngaymua = ds2.Tables[0].Rows[0][4].ToString()[0].ToString();
                for (int j = 2; j < ds1.Tables[0].Rows[0][2].ToString().Length ; j++)
                {
                    toake2 += ds1.Tables[0].Rows[0][2].ToString()[j].ToString();
                }
                t = ds1.Tables[0].Rows[0][2].ToString()[0].ToString();
                toake1 += t;
                toake1  += ds1.Tables[0].Rows[0][2].ToString()[1].ToString();
                temp = int.Parse(toake1) + int.Parse(Ngaymua);
                toake = temp.ToString() + toake2;
                for (int j = 0; j < ds1.Tables[0].Rows.Count; j++)
                {
                    if ((toake == ds1.Tables[0].Rows[j][2].ToString()) && (mabn == ds1.Tables[0].Rows[j][1].ToString()))
                    {
                        matoa = ds1.Tables[0].Rows[j][0].ToString();
                        for (int k = 0; k < ds2.Tables[0].Rows.Count; k++)
                        {
                            tenthuoc += ds2.Tables[0].Rows[k][2].ToString();
                            if (k != ds2.Tables[0].Rows.Count - 1)
                            {
                                tenthuoc += "#";
                            }
                        }
                    }
                }
                String sql3 = "Select * from ChiTietToaThuoc where MaToa = " + matoa;
                ds3 = dbc.executeQuery(sql3);
                for (int k = 0; k < ds3.Tables[0].Rows.Count; k++)
                {
                    tenthuoc1 += ds3.Tables[0].Rows[k][2].ToString();
                    if (k != ds3.Tables[0].Rows.Count - 1)
                    {
                        tenthuoc1 += "#";
                    }
                }

                string[] a1 = tenthuoc.Split('#');
                string[] a2 = tenthuoc1.Split('#');
                string[] b1 = new string[counttl];
                b1 = loai_trung_lap(a1);
                for (int l = 0; l < counttl; l++)
                {
                    b1[l] = a1[l];
                }
                string[] b2 = new string[counttl];
                b2 = loai_trung_lap(a2);
                for (int l = 0; l < counttl; l++)
                {
                    b2[l] = a2[l];
                }

                Boolean ss = false;
                if (tenthuoc != "")
                {
                    for (int j = 0; j < a.Length; j++)
                    {
                        for (int k = 0; k < b1.Length; k++)
                        {
                            if (a[j] == b1[k])
                            {
                                for(int l = 0; l < a.Length ; l++)
                                {
                                    for (int m = 0; m < b2.Length; m++)
                                    {
                                        if (a[l] == b2[m])
                                        {
                                            if (b1[k] == b2[m])
                                            {
                                                ss = true;
                                            }
                                            else
                                            {
                                                t = b1[k];
                                                bn.Khangthuoc = ds.Tables[0].Rows[0][7].ToString() + ',' + t;

                                                string[]  temp1 = bn.Khangthuoc.Split(',');
                                                string[] temp2 = loai_trung_lap(temp1);
                                                bn.Khangthuoc = "";
                                                for (int n = 0; n < temp2.Length; n++)
                                                {
                                                    bn.Khangthuoc += temp2[n];
                                                    if (n != temp2.Length - 1)
                                                    {
                                                        bn.Khangthuoc += ',';
                                                    }
                                                }
                                                txtkhangthuoc.Text = bn.Khangthuoc ;
                                                bn.Cap_Nhat_Khang_Thuoc(mabn);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            





                this.txtghichu.Text = ds.Tables[0].Rows[0][8].ToString();
            //Phan hien thi cac loai thuoc dap ung
            sql = "Select Tot, Vua, Khong from DapUng where MaBN = " + mabn;
            ds = dbc.executeQuery(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                this.txtdugtot.Text = ds.Tables[0].Rows[0][0].ToString();
                this.txtdugvua.Text = ds.Tables[0].Rows[0][1].ToString();
                this.txtdugkhong.Text = ds.Tables[0].Rows[0][2].ToString();
            }
            else
            {
                this.txtdugtot.Text = "";
                this.txtdugvua.Text = "";
                this.txtdugkhong.Text = "";
            }
            ////Phan hien thi cac loai thuoc phoi hop
            sql = "Select PHMot, PHHai,PHBa From PhoiHop where MaBN = " + mabn;
            ds = dbc.executeQuery(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                this.txtphlanmot.Text = ds.Tables[0].Rows[0][0].ToString();
                this.txtphlanhai.Text = ds.Tables[0].Rows[0][1].ToString();
                this.txtphlanba.Text = ds.Tables[0].Rows[0][2].ToString();
            }
            else
            {
                this.txtphlanmot.Text = "";
                this.txtphlanhai.Text = "";
                this.txtphlanba.Text = "";
            }
            //Phan hien thi thong tin 2 toa thuoc moi nhat
            sql = "Select TOP 2 MaToa from ToaThuoc where MaBN = " + mabn;
            sql += " Order By MaToa DESC";
            ds = dbc.executeQuery(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows.Count == 1)
                {
                    String matoake = ds.Tables[0].Rows[0][0].ToString();
                    sql = "Select TenThuoc, LieuDung, ThoiGian, GhiChu from ChiTietToaThuoc ";
                    sql += "where MaToa = " + matoake;
                    ds = dbc.executeQuery(sql);
                    this.dgrvToa1.DataSource = ds.Tables[0];
                    //this.dgrvToa1.DataSource = null;
                }
                else if (ds.Tables[0].Rows.Count == 2)
                {
                    String matoa1 = ds.Tables[0].Rows[0][0].ToString();
                    String matoa2 = ds.Tables[0].Rows[1][0].ToString();
                    sql = "Select TenThuoc, LieuDung, ThoiGian, GhiChu from ChiTietToaThuoc ";
                    sql += "where MaToa = " + matoa1;
                    ds = dbc.executeQuery(sql);
                    this.dgrvToa1.DataSource = ds.Tables[0];

                    sql = "Select TenThuoc, LieuDung, ThoiGian, GhiChu from ChiTietToaThuoc ";
                    sql += "where MaToa = " + matoa2;
                    ds = dbc.executeQuery(sql);
                    this.dgrvToa2.DataSource = ds.Tables[0];
                }

            }
            else
            {
                this.dgrvToa1.DataSource = null;
                this.dgrvToa2.DataSource = null;
            }

            ds = bn.Lay_Danh_Sach_Toa(mabn);
            if (ds.Tables[0].Rows.Count > 0)
            {
                dgvtoathuoc.DataSource = ds.Tables[0];
                dgvchitiettoa.AllowUserToAddRows = true;
            }
            else
            {
                masktxtdate.Text = "";

                ds = bn.Lay_Danh_Sach_Toa("-1");
                dgvtoathuoc.DataSource = ds.Tables[0];

                ds = bn.Lay_Danh_Sach_Chi_Tiet_Toa("-1");
                dgvchitiettoa.DataSource = ds.Tables[0];
                dgvchitiettoa.AllowUserToAddRows = false;
            }
            cksuadl = false;
            BenhNhan.cksuadl = false;
        }

        int counttl;
        string[] k;



        internal string[] loai_trung_lap( string[] a)
        {
            counttl = 0;
            
            for (int i = 0; i < a.Length - 1; i++)
            {
                for(int j = i + 1 ; j < a.Length ; j++)
                {
                    if (a[i] == a[j])
                    {
                        counttl++;
                        k = new string[a.Length - counttl];
                        for (int l = j; l < a.Length; l++)
                        {
                            a[l] = a[l - 1];
                        }
                        for (int l = 0; l < a.Length - counttl; l++)
                        {
                            k[l] = a[l];
                        }
                        loai_trung_lap(k);
                    }
                }
            }
            counttl = k.Length;
            return k;
        }





        internal void Cap_Nhat_Thong_Tin_Benh_Nhan()
        {
            //Phan tab thong tin benh nhan
            BenhNhan bn = new BenhNhan();
            bn.Mabn = this.txtmabn.Text;
            bn.Tenbn = this.txttenbn.Text;
            String str = this.txttuoibn.Text;

            String d = str.Substring(0, 2);
            String m = str.Substring(3, 2);
            String y = str.Substring(6, 4);

            bn.Tuoibn = m + "/" + d + "/" + y;
            //bn.Tuoibn = this.txttuoibn.Text;
            bn.Cannang = this.txtcannang.Text;
            bn.Diachi = this.txtdiachi.Text;
            bn.Benhsu = this.txtbenhsu.Text;
            bn.Diung = this.txtdiung.Text;
            bn.Khangthuoc = this.txtkhangthuoc.Text;
            bn.Ghichu = this.txtghichu.Text;
            bn.Tot = this.txtdugtot.Text;
            bn.Vua = this.txtdugvua.Text;
            bn.Khong = this.txtdugkhong.Text;
            bn.Lanba = this.txtphlanmot.Text;
            bn.Lanhai = this.txtphlanhai.Text;
            bn.Lanba = this.txtphlanba.Text;
            bn.PhMot = this.txtphlanmot.Text;
            bn.PhHai = this.txtphlanhai.Text;
            bn.PhBa = this.txtphlanba.Text;

            Boolean ck1 = true;
            Boolean ck2 = true;

            if (int.Parse(d) > 31 || int.Parse(m) < 1)
            {
                MessageBox.Show("Ngày sinh bệnh nhân sai!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ck2 = false;
            }
            else if (int.Parse(m) > 12 || int.Parse(m) < 1)
            {
                
                MessageBox.Show("Tháng sinh bệnh nhân sai!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ck2 = false;
              
            }
            
            else if (bn.Tenbn == "")
            {
                MessageBox.Show("Xin nhập tên bệnh nhân!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ck2 = false;
            }
            else if (bn.Tuoibn == "")
            {
                MessageBox.Show("Xin nhập tuổi bệnh nhân!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ck2 = false;
            }
            else if (bn.Cannang == "")
            {
                MessageBox.Show("Xin nhập chỉ số cân nặng bệnh nhân!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ck2 = false;
            } 

            else if (bn.Diachi == "")
            {
                MessageBox.Show("Xin nhập địa chỉ bệnh nhân!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ck2 = false;
            }

            if (bn.Cannang != "")
            {
                Double kq;
                Boolean kt = Double.TryParse(bn.Cannang, out kq);
                if (kt == false)
                {
                    MessageBox.Show("Xin xem lại chỉ số cân nặng!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    ck1 = false;
                }
            }

            if(ck1 == true && ck2 == true)
            {
                //MessageBox.Show(bn.Mabn);

                if (bn.Cap_Nhat_Thong_Tin())
                {
                    MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Lỗi trong qua trình cập nhật. Xin kiểm tra lại dữ liệu!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                if (txtmabn.Text != "")
                {
                    Hien_Thong_Tin(txtmabn.Text);
                }
                else
                {
                    String sql = "Select Max(MaBN) From BenhNhan";
                    Hien_Thong_Tin(Convert.ToString(dbc.executeScalar(sql)));
                }
                    
            }
            
        }
        internal void Cap_Nhat_Toa_Thuoc()
        {
            if (txtmabn.Text != "")
            {
                this.masktxtdate.Focus();
                BenhNhan bn = new BenhNhan();
                String str = this.masktxtdate.Text;
                String d = str.Substring(0, 2);
                String m = str.Substring(3, 2);
                String y = str.Substring(6, 4);
                bn.NgayKham = m + "/" + d + "/" + y;
                if (ck == false)
                {
                    /*Cap nhat ngay kham*/
                    String maToa = dgvtoathuoc.SelectedRows[0].Cells[0].Value.ToString();
                    bn.Cap_Nhat_Toa(maToa);
                    /*------------------*/
                    if (dgvchitiettoa.Rows.Count > 0)
                    {
                        bn.MaToa = dgvtoathuoc.SelectedRows[0].Cells[0].Value.ToString();
                        String sql = "Select Count(MaCTT) From ChiTietToaThuoc Where MaToa = " + bn.MaToa;
                        int k = Convert.ToInt32(dbc.executeScalar(sql).ToString());
                        for (int i = 0; i < k; i++)
                        {

                            String maCTT = dgvchitiettoa.Rows[i].Cells[4].Value.ToString();
                            bn.TenThuoc = dgvchitiettoa.Rows[i].Cells[0].Value.ToString();
                            bn.LieuDung = dgvchitiettoa.Rows[i].Cells[1].Value.ToString();
                            bn.ThoiGian = dgvchitiettoa.Rows[i].Cells[2].Value.ToString();
                            bn.GhiChuToa = dgvchitiettoa.Rows[i].Cells[3].Value.ToString();
                            bn.Cap_Nhat_Toa_Thuoc(maCTT);
                            //if (bn.Cap_Nhat_Toa_Thuoc(maCTT) == true)
                            //{
                            //    MessageBox.Show("Cập nhật toa thuốc thành công !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                            //}
                        }
                        for (int i = k; i < dgvchitiettoa.Rows.Count - 1; i++)
                        {
                            String maCTT = dgvchitiettoa.Rows[i].Cells[4].Value.ToString();
                            bn.TenThuoc = dgvchitiettoa.Rows[i].Cells[0].Value.ToString();
                            bn.LieuDung = dgvchitiettoa.Rows[i].Cells[1].Value.ToString();
                            bn.ThoiGian = dgvchitiettoa.Rows[i].Cells[2].Value.ToString();
                            bn.GhiChuToa = dgvchitiettoa.Rows[i].Cells[3].Value.ToString();
                            bn.Them_Chi_Tiet_Toa();
                        }
                        //ck = false;
                    }
                    MessageBox.Show("Cập nhật toa thuốc thành công !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                }
                else
                {
                    bn.Mabn = txtmabn.Text;
                    bn.Them_Toa_Thuoc();
                    String sql = "Select Max(MaToa) From ToaThuoc";
                    bn.MaToa = Convert.ToString(dbc.executeScalar(sql));
                    for (int i = 0; i < dgvchitiettoa.Rows.Count - 1; i++)
                    {
                        //String maCTT = dgvchitiettoa.Rows[i].Cells[4].Value.ToString();
                        bn.TenThuoc = dgvchitiettoa.Rows[i].Cells[0].Value.ToString();
                        bn.LieuDung = dgvchitiettoa.Rows[i].Cells[1].Value.ToString();
                        bn.ThoiGian = dgvchitiettoa.Rows[i].Cells[2].Value.ToString();
                        bn.GhiChuToa = dgvchitiettoa.Rows[i].Cells[3].Value.ToString();
                        bn.Them_Chi_Tiet_Toa();
                    }
                    dgvtoathuoc.DataSource = ((DataSet)bn.Lay_Danh_Sach_Toa(bn.Mabn)).Tables[0];
                    dgvchitiettoa.DataSource = ((DataSet)bn.Lay_Danh_Sach_Chi_Tiet_Toa(bn.MaToa)).Tables[0];
                    ck = false;
                    MessageBox.Show("Cập nhật toa thuốc thành công !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                }
                Hien_Thong_Tin();
                dgvtoathuoc.Focus();
            }
            else
            {
                MessageBox.Show("Xin nhập thông tin bệnh nhân!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        internal void Soan_Toa_Thuoc()
        {
            if (txtmabn.Text != "")
            {
                ck = true;
                BenhNhan bn = new BenhNhan();
                DataSet ds = bn.Lay_Danh_Sach_Chi_Tiet_Toa("-1");
                dgvchitiettoa.DataSource = ds.Tables[0];

                DateTime d = DateTime.Today;
                String str = "";
                if (d.Day < 10)
                {
                    str += "0" + d.Day;
                }
                else
                {
                    str += d.Day;
                }
                if (d.Month < 10)
                {
                    str += "0" + d.Month;
                }
                else
                {
                    str += d.Month;
                }
                str += d.Year;
                masktxtdate.Text = str;
                dgvchitiettoa.AllowUserToAddRows = true;
                dgvchitiettoa.Focus();

            }
            else
            {
                dgvchitiettoa.AllowUserToAddRows = false;
                MessageBox.Show("Xin nhập thông tin bệnh nhân!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        internal void btncapnhat_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(tabCtrbenhnhan.SelectedIndex.ToString());
            //BenhNhan bn = new BenhNhan();
            //bn.Mabn = this.txtmabn.Text;
            //bn.Tenbn = this.txttenbn.Text;
            //bn.Tuoibn = this.txttuoibn.Text;
            //bn.Cannang = this.txtcannang.Text;
            //bn.Diachi = this.txtdiachi.Text;
            //bn.Benhsu = this.txtbenhsu.Text;
            //bn.Diung = this.txtdiung.Text;
            //bn.Khangthuoc = this.txtkhangthuoc.Text;
            //bn.Ghichu = this.txtghichu.Text;
            //bn.Tot = this.txtdugtot.Text;
            //bn.Vua = this.txtdugvua.Text;
            //bn.Khong = this.txtdugkhong.Text;
            //bn.Lanba = this.txtphlanmot.Text;
            //bn.Lanhai = this.txtphlanhai.Text;
            //bn.Lanba = this.txtphlanba.Text;

            //if (bn.Tenbn == "")
            //{
            //    MessageBox.Show("Xin nhập tên bệnh nhân!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //}
            //else if (bn.Tuoibn == "")
            //{
            //    MessageBox.Show("Xin nhập tuổi bệnh nhân!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //}
            //else if (bn.Cannang == "")
            //{
            //    MessageBox.Show("Xin nhập chỉ số cân nặng bệnh nhân!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //}
            //else if (bn.Diachi == "")
            //{
            //    MessageBox.Show("Xin nhập địa chỉ bệnh nhân!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //}
            //else
            //{
            //    //MessageBox.Show(bn.Mabn);
            //    if (bn.Cap_Nhat_Thong_Tin())
            //    {
            //        MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    }
            //    else
            //    {
            //        MessageBox.Show("Lỗi trong qua trình cập nhật. Xin kiểm tra lại dữ liệu!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    }
            //}
        }

        private void Xoa_Form()
        {
            BenhNhan bn = new BenhNhan();
            DataSet ds = null;
            this.txtmabn.Text = "";
            //this.txttenbn.Text = "";
            this.txttuoibn.Text = "";
            //this.txttuoibn1.Text = "";
            this.txtcannang.Text = "";
            this.txtdiachi.Text = "";
            this.txtbenhsu.Text = "";
            this.txtdiung.Text = "";
            this.txtkhangthuoc.Text = "";
            this.txtghichu.Text = "";

            this.txtdugtot.Text = "";
            this.txtdugvua.Text = "";
            this.txtdugkhong.Text = "";

            this.txtphlanmot.Text = "";
            this.txtphlanhai.Text = "";
            this.txtphlanba.Text = "";

            this.dgrvToa1.DataSource = null;
            this.dgrvToa2.DataSource = null;

            masktxtdate.Text = "";
            ds = bn.Lay_Danh_Sach_Toa("-1");
            dgvtoathuoc.DataSource = ds.Tables[0];

            ds = bn.Lay_Danh_Sach_Chi_Tiet_Toa("-1");
            dgvchitiettoa.DataSource = ds.Tables[0];
            dgvchitiettoa.AllowUserToAddRows = false;
        }


        private void frmTTBenhNhan_Load(object sender, EventArgs e)
        {
            frmParent.timbn_MenuItem.Enabled = false;
            cksuadl = false;
            BenhNhan.cksuadl = false;
            this.txttenbn.Select();
            this.dgvchitiettoa.AllowUserToAddRows = false;
        }

        private void lbox_tenbn_MouseClick(object sender, MouseEventArgs e)
        {
            if (lbox_tenbn.SelectedIndex != -1)
            {
                this.txttenbn.Text = this.lbox_tenbn.SelectedItem.ToString();
            }
        }

        private void lbox_tenbn_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (lbox_tenbn.SelectedIndex != -1)
            {
                lbox_tenbn.Visible = false;
                txttenbn.Focus();
                Hien_Thong_Tin();
            }
            
        }

        private void dgvtoathuoc_SelectionChanged(object sender, EventArgs e)
        {
            BenhNhan bn = new BenhNhan();
            if (dgvtoathuoc.SelectedRows.Count > 0)
            {
                String maToa = dgvtoathuoc.SelectedRows[0].Cells[0].Value.ToString();
                DateTime d = Convert.ToDateTime(dgvtoathuoc.SelectedRows[0].Cells[1].Value);
                String str ="";
                if (d.Day < 10)
                {
                    str += "0" + d.Day;
                }
                else
                {
                    str += d.Day;
                }
                if(d.Month <10)
                {
                    str += "0" + d.Month;
                }
                else
                {
                    str += d.Month;
                }
                str += d.Year;
                masktxtdate.Text = str;
                
                DataSet ds = bn.Lay_Danh_Sach_Chi_Tiet_Toa(maToa);
                dgvchitiettoa.DataSource = ds.Tables[0];
                ck = false;
            }
        }

        private void dgvchitiettoa_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            if (dgvchitiettoa.Focused)
            {
                if (MessageBox.Show("Bạn thật sự muốn xóa !", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.No)
                {
                    e.Cancel = true;
                }
                else
                {
                    if (dgvchitiettoa.SelectedRows[0].Cells[4].Value.ToString() != "")
                    {
                        BenhNhan bn = new BenhNhan();
                        bn.Xoa_Chi_Tiet_Toa_Thuoc(dgvchitiettoa.SelectedRows[0].Cells[4].Value.ToString());
                        Hien_Thong_Tin();
                    }
                }
            }
        }

        private void lbCal_Click(object sender, EventArgs e)
        {
            if (calen.Visible == true)
            {
                calen.Visible = false;
            }
            else
            {
                calen.Visible = true;
            }
        }

        private void calen_DateSelected(object sender, DateRangeEventArgs e)
        {
            DateTime d = calen.SelectionStart;
            String str = "";
            if (d.Day < 10)
            {
                str += "0" + d.Day;
            }
            else
            {
                str += d.Day;
            }
            if (d.Month < 10)
            {
                str += "0" + d.Month;
            }
            else
            {
                str += d.Month;
            }
            str += d.Year;
            masktxtdate.Text = str;
            calen.Visible = false;
        }

        private void txtbenhsu_TextChanged(object sender, EventArgs e)
        {
            cksuadl = true;
            BenhNhan.cksuadl = true;
        }

        private void frmTTBenhNhan_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmParent.ckfrmbenhnhanactive = false;
            frmParent.saveMenuItem.Enabled = false;
            frmParent.timbn_MenuItem.Enabled = true;
            frmParent.Soantoathuoc_MenuItem.Enabled = false;
            frmParent.changeToolStripMenuItem.Enabled = false;
        }

        private void tabCtrbenhnhan_SelectedIndexChanged(object sender, EventArgs e)
        {
            Save();

            if (tabCtrbenhnhan.SelectedIndex == 0)
            {
                cktab = 0;
                frmParent.Soantoathuoc_MenuItem.Enabled = false;
                frmParent.ThemBNToolStripMenuItem.Enabled = true;
                Setfocus_Ten_Benh_Nhan();
            }
            else if (tabCtrbenhnhan.SelectedIndex == 1)
            {
                cktab = 1;
                frmParent.Soantoathuoc_MenuItem.Enabled = true;
                frmParent.ThemBNToolStripMenuItem.Enabled = false;
                dgvtoathuoc.Focus();
            }
        }

        public int count = 0;
        internal void Save()
        {
            counttl += 1;
            if (counttl == 1)
            {
                cksuadl = false;
            }
            if (cksuadl == true)
            {
                if (MessageBox.Show("Bạn có muốn lưu lại dữ liệu !", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                {
                    if (tabCtrbenhnhan.SelectedIndex == 0)
                    {
                        Cap_Nhat_Toa_Thuoc();
                        cksuadl = false;
                        BenhNhan.cksuadl = false;
                    }
                    else if (tabCtrbenhnhan.SelectedIndex == 1)
                    {
                        Cap_Nhat_Thong_Tin_Benh_Nhan();
                        cksuadl = false;
                        BenhNhan.cksuadl = false;
                    }
                }
                else
                {
                    cksuadl = false;
                    BenhNhan.cksuadl = false;
                }
            }
        }
        internal void Setfocus_Ten_Benh_Nhan()
        {
            //this.txttenbn.Focus();
            this.lbox_tenbn.Visible = false;
        }

        internal void Setfocus_Select_Ten_Benh_Nhan()
        {
            this.txttenbn.Focus();
            this.txttenbn.Text = "";
            //this.lbox_tenbn.Visible = false;
        }

        private void txttuoibn_TextChanged(object sender, EventArgs e)
        {
            cksuadl = true;
            BenhNhan.cksuadl = true;
        }

        private void txtcannang_TextChanged(object sender, EventArgs e)
        {
            cksuadl = true;
            BenhNhan.cksuadl = true;
        }

        private void txtdiachi_TextChanged(object sender, EventArgs e)
        {
            cksuadl = true;
            BenhNhan.cksuadl = true;
        }

        private void txtghichu_TextChanged(object sender, EventArgs e)
        {
            cksuadl = true;
            BenhNhan.cksuadl = true;
        }

        private void txtdiung_TextChanged(object sender, EventArgs e)
        {
            cksuadl = true;
            BenhNhan.cksuadl = true;
        }

        private void txtkhangthuoc_TextChanged(object sender, EventArgs e)
        {
            cksuadl = true;
            BenhNhan.cksuadl = true;
        }

        private void txtdugtot_TextChanged(object sender, EventArgs e)
        {
            cksuadl = true;
            BenhNhan.cksuadl = true;
        }

        private void txtdugvua_TextChanged(object sender, EventArgs e)
        {
            cksuadl = true;
            BenhNhan.cksuadl = true;
        }

        private void txtdugkhong_TextChanged(object sender, EventArgs e)
        {
            cksuadl = true;
            BenhNhan.cksuadl = true;
        }

        private void txtphlanmot_TextChanged(object sender, EventArgs e)
        {
            cksuadl = true;
            BenhNhan.cksuadl = true;
        }

        private void txtphlanhai_TextChanged(object sender, EventArgs e)
        {
            cksuadl = true;
            BenhNhan.cksuadl = true;
        }

        private void txtphlanba_TextChanged(object sender, EventArgs e)
        {
            cksuadl = true;
            BenhNhan.cksuadl = true;
        }

        private void masktxtdate_TextChanged(object sender, EventArgs e)
        {
            cksuadl = true;
            BenhNhan.cksuadl = true;
        }

        private void txttenbn_TextChanged(object sender, EventArgs e)
        {
            cksuadl = true;
            BenhNhan.cksuadl = true;
        }

        private void frmTTBenhNhan_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (cksuadl == true)
            {
                if (MessageBox.Show("Bạn có muốn lưu lại dữ liệu !", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                {
                    if (tabCtrbenhnhan.SelectedIndex == 0)
                    {
                        Cap_Nhat_Thong_Tin_Benh_Nhan();
                        cksuadl = false;
                        BenhNhan.cksuadl = false;
                    }
                    else if (tabCtrbenhnhan.SelectedIndex == 1)
                    {
                        Cap_Nhat_Toa_Thuoc();
                        cksuadl = false;
                        BenhNhan.cksuadl = false;
                    }
                }
                else
                {
                    cksuadl = false;
                    BenhNhan.cksuadl = false;
                }
            }
        }

        private void frmTTBenhNhan_VisibleChanged(object sender, EventArgs e)
        {
           
        }

        private void txttuoibn_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {

        }

        private void label17_Click(object sender, EventArgs e)
        {
            bcalen.BringToFront();
            if (bcalen.Visible == true)
            {
                bcalen.Visible = false;
            }
            else
            {
                bcalen.Visible = true;
            }
        }

        private void grbtinhtrang_Enter(object sender, EventArgs e)
        {

        }

        private void bcalen_DateChanged(object sender, DateRangeEventArgs e)
        {
           
        }

        private void dgrvToa1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dgvtoathuoc_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {

        }

        private void bcalen_DateSelected(object sender, DateRangeEventArgs e)
        {
            DateTime d = bcalen.SelectionStart;
            String str = "";
            if (d.Day < 10)
            {
                str += "0" + d.Day;
            }
            else
            {
                str += d.Day;
            }
            if (d.Month < 10)
            {
                str += "0" + d.Month;
            }
            else
            {
                str += d.Month;
            }
            str += d.Year;
            txttuoibn.Text = str;
            bcalen.Visible = false;
        }

        private void dgvchitiettoa_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            cksuadl = true;
            BenhNhan.cksuadl = true;
        }


    }
}
